package com.sxran.im.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sxran.im.bean.GenerateConversationIdReqBody;
import com.sxran.im.bean.MessageReqBody;
import com.sxran.im.bean.NotificationBody;
import com.sxran.im.bean.UserMessageData;
import com.sxran.im.dao.ImChatBodyMapper;
import com.sxran.im.dao.ImConversationMapper;
import com.sxran.im.dao.ImMsgExtrasMapper;
import com.sxran.im.dao.ImNotificationMapper;
import com.sxran.im.feign.ImApiFeign;
import com.sxran.im.feign.dto.ImRevokeMessageDto;
import com.sxran.im.handler.*;
import com.sxran.im.info.*;
import com.sxran.im.service.ChatService;
import com.sxran.im.service.ConversationService;
import com.sxran.im.service.NotificationService;
import com.sxran.im.service.UserGroupService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * 通知内容
 */
@CrossOrigin(origins = "*")
@RestController
public class NotificationController {
	@Resource
	private ImApiFeign imApiFeign;
	@Resource
	private ImNotificationMapper imNotificationMapper;
	@Resource
	private ConversationService conversationService;
	@Resource
	private NotificationService notificationService;
	///////////////////////////////////////////////////////////
	////                      带有通知的接口
	////
	////////////////////////////////////////////////////////////


	///////////////////////////////////////////////////////////
	////		传统的im业务...
	////
	////////////////////////////////////////////////////////////


	/**
	 * 获得消息列表
	 * @see NotificationAfterCmdHandler
	 * @param json
	 * @return
	 */
	@PostMapping("/api/notification/sendNotification")
	public CommonResult<Integer> sendNotification(@RequestBody NotificationBody json) {
		// 存表....
		ImNotification imNotification = new ImNotification();
		Integer type = json.getType();
		// 这个可以选择不存数据库。
		if (ArrayUtil.contains(new Integer[]{46},type)) {
			return  CommonResult.success(0);
		}
		imNotification.setType(type);
		imNotification.setUuid(json.getUuid());
		imNotification.setIsRead(0);
		ImUser from = json.getFrom();
		imNotification.setFromUid(from.getUserId());
		imNotification.setFromInfo(JSONObject.toJSONString(from));
		imNotification.setToId(ArrayUtil.get(json.getTargetList(),0)  );
		imNotification.setServiceCreateTime(new Date(json.getServiceCreateTime()));
		imNotification.setContent(json.getContent().toString());
		ImNotification i = notificationService.updateOrInsert(imNotification);
		return  CommonResult.success(1);
	}

	/**
	 * 清除标记数量
	 * @see ClearUnreadCountCmdHandler
	 * @param json
	 * @return
	 */
	@PostMapping("/api/notification/clearUnreadCount")
	public CommonResult<Integer> clearUnreadCount(@RequestBody ImConversation json) {
		conversationService.clearUnreadCount(json.getUserId(),json.getId());
        // 根据后台规则 生成会话id 等 内容
		return  CommonResult.success(0);
	}





	/**
	 * 根据后台规则 生成会话id 等 内容
	 * @see ReadNotificationCmdHandler
	 * @param json
	 * @return
	 */
	@PostMapping("/api/notification/read")
	public CommonResult<Integer> read(@RequestBody ImNotification json){
		// 删除对应的会话内容。
		LambdaQueryWrapper<ImNotification> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(ImNotification::getUuid,json.getUuid());
		queryWrapper.eq(ImNotification::getToId,json.getToId());
		//
		ImNotification imFriendInvite = imNotificationMapper.selectOne(queryWrapper);
		int i = 0;
		if (imFriendInvite != null) {
			imFriendInvite.setIsRead(1);
			i = imNotificationMapper.update(imFriendInvite,queryWrapper);
		}
		// 根据后台规则 生成会话id 等 内容
		return  CommonResult.success(i);
	}
	/**
	 * @see GetNotificationListCmdHandler
	 * @param bean
	 * @return
	 */
	@PostMapping("/api/notification/getNotificationList")
	public CommonResult<List<ImNotification>> getNotificationList(@RequestBody ImNotification bean){
		LambdaQueryWrapper<ImNotification> queryWrapper = new LambdaQueryWrapper<>(bean);
		List<ImNotification> list = imNotificationMapper.selectList(queryWrapper);
		// 扩展字段的使用。
		list.forEach(x -> { x.setExtras(JSONObject.parseObject(x.getSpare())); });
		return CommonResult.success(list);
	}

}
